#include<stdio.h>
#include<sys/shm.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<stdlib.h>
#include "command.h"
 
int main()
{
	int i=0;
	ckx *p;
	int id = 0;
	while(1)
	{
		printf("\n\n1.input data to sharememory\n2.get sharememory data\n\3.destroy sharememory\ninput select:");
		scanf("%d",&i);
		if(i > 3 |i< 1)
		{
			printf("input error\n");
			continue;
		}
		
		id = get_ipc(sizeof(ckx));
	//	printf("id: %d\n",id);
		if(id < 0)
		{
			printf("get sharememory error\n");
			break;
		}
		p = (ckx *)shmat(id,NULL,0);
		if(p < 0)
		{
			printf("get sharememory addr error\n");
			p = NULL;
			break;
		}
		
		if(i == 1)
		{  //     printf("i=1.id:%d\n",id);
			char name[NAME_LEN];
			int age =0;
			getchar();//不要动否则后果自负
			printf("input name:");
			fgets(name,NAME_LEN,stdin);
			printf("input age:");
			scanf("%d",&age);
			
			strcpy(p->name,name);
			p->age = age;
			printf("write success\n");
 
			if(shmdt(p) == -1)
			{
				printf("shmdt error\n");
			}
			id = 0;
		}
		if(i ==  2)
		{    //   printf("i=2 id:%d\n",id);
			printf("name:%s \t age:%d\n",p->name,p->age);
			if(shmdt(p) == -1)
			{
				printf("shmdt error\n");
				break;
			}
			id = 0;
		}
		if(i == 3)
		{      // printf("i=3,id:%d\n",id);
			if(shmdt(p) == -1)
			{
				printf("shmdt error\n");
				break;
			}
			break;
		}
	}
	if(id !=0)
	{
		if(destroy_sharememory(id)<0)
		{
			printf("destroy error\n");
		}
	}
	
	
}
